# -*- coding: utf-8 -*-
"""
Created on Mon Sep 20 12:57:04 2021
@author: 86157
"""
import lxml.html
import requests as rq
import pandas as pd
from time import sleep
header = {'User-Agent':'Safari/537.36'} #伪装浏览器

range = [0,10,20,30,40,50,60,70,80,90] #获取每一页的电影信息
L1 = []
L2 = []
L3 = []
L4 = []
L5 = []
L6 = []
L7 = []
for i in range:
    url='https://maoyan.com/board/4?offset='+str(i) #获取网页 100个榜单
    sources = rq.get(url,headers = header)
    type(sources)
    data= sources.text #网页源代码
    selector=lxml.html.fromstring(data)

    #获取电影排名
    pm = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/i/text()') #获取文本内容用text()
    #获取电影标题
    #bt = selector.xpath('//*[@id="app"]/div/div/div[1]/dl/dd[1]/div/div/div[1]/p[1]/a/text()') #这里只爬取了排名第一的电影名称
    bt = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/div/div/div/p/a/text()') #把[1]删掉后就是爬取一整页的，即10部电影
    #获取主演
    zy = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/div/div/div/p[2]/text()') #p[2]是只获取主演，删掉[2]就是获取了主演和上映时间
    #获取上映时间
    sj = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/div/div/div/p[3]/text()')
    #获取评分
    pf1 = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/div/div/div/p/i[1]/text()') #获取小数点及小数点前的数
    pf2 = selector.xpath('//*[@id="app"]/div/div/div/dl/dd/div/div/div/p/i[2]/text()') #获取小数点后的数
    L1 = L1 + pm
    L2 = L2 + bt
    L3 = L3 + zy
    L4 = L4 + sj 
    L5 = L5 + pf1
    L6 = L6 + pf2
    L7 = L5 + L6
    sleep(25) #设置获取数据的时间

D=pd.DataFrame({'榜单排名':L1,'电影名':L2,'主演':L3,'上映时间':L4,'评分':L7})



